package com.study.scapp.info.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.scapp.info.common.CommonResult;
import com.study.scapp.info.pojo.InfoCommodity;
import com.study.scapp.info.pojo.InfoShoppingCart;
import com.study.scapp.info.pojo.vo.ShoppingCartVo;
import com.study.scapp.info.service.InfoBusinessService;
import com.study.scapp.info.service.InfoCommodityService;
import com.study.scapp.info.service.InfoShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 1
 * @data 2023/5/27
 */
@RestController
@RequestMapping("/shoppingCart")
public class InfoShoppingCartController {

    @Resource
    private InfoShoppingCartService infoShoppingCartService;

    @Resource
    private InfoCommodityService infoCommodityService;

    @Resource
    private InfoBusinessService infoBusinessService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @param request
     * @return
     */
    @PostMapping("/addCart")
    public CommonResult addCart(@RequestParam Map<String, Object> shoppingCart, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(shoppingCart);
        InfoShoppingCart infoShoppingCart = jsonObject.toJavaObject(InfoShoppingCart.class);

        //存入用户
        String userId = request.getHeader("user_id");
        infoShoppingCart.setUserId(Long.valueOf(userId));
        InfoShoppingCart one = infoShoppingCartService.getOne(new QueryWrapper<InfoShoppingCart>().eq("commodity_id", infoShoppingCart.getCommodityId()).eq("user_id", userId));
        if (one != null){
            one.setCommodityNum(one.getCommodityNum()+1);
            infoShoppingCartService.saveOrUpdate(one);
            return CommonResult.success("添加成功！");
        }
        InfoCommodity byId = infoCommodityService.getById(infoShoppingCart.getCommodityId());
        infoShoppingCart.setBusinessName(byId.getBusinessName());
        infoShoppingCart.setCommodityPrice(byId.getCommodityPrice());
        infoShoppingCartService.save(infoShoppingCart);
        return CommonResult.success("添加成功！");
    }

    /**
     * 列表
     * @return
     */
    @GetMapping("/listCart")
    public CommonResult listShoppingCart(HttpServletRequest request){
        String userId = request.getHeader("user_id");
        ArrayList<ShoppingCartVo> shoppingCartVos1 = new ArrayList<>();
        List<InfoShoppingCart> shoppingCarts = infoShoppingCartService.list(new QueryWrapper<InfoShoppingCart>().eq("user_id", userId));
        shoppingCarts.stream().forEach(x->{
            ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
            BeanUtils.copyProperties(x,shoppingCartVo);
            InfoCommodity byId = infoCommodityService.getById(x.getCommodityId());
            shoppingCartVo.setCommodityName(byId.getCommodityName());
            shoppingCartVo.setCommodityIntroduce(byId.getCommodityIntroduce());
            shoppingCartVo.setImg1(byId.getImg1());
            shoppingCartVos1.add(shoppingCartVo);
        });
        //分组 根据商家
        Map<String, List<ShoppingCartVo>> collect = shoppingCartVos1.stream().collect(Collectors.groupingBy(ShoppingCartVo::getBusinessName));

        Set<Map.Entry<String, List<ShoppingCartVo>>> entries = collect.entrySet();
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        entries.stream().forEach(entry->{
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("title", entry.getKey());
                    map.put("list", entry.getValue());
                    maps.add(map);
                }
                );

        return CommonResult.success(maps);
    }

    /**
     * 删除购物车
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCart/{id}")
    public CommonResult deleteShoppingCart(@PathVariable("id") Long id){
        if (infoShoppingCartService.removeById(id)){
            return CommonResult.success("删除成功！");
        }else {
            return CommonResult.failed("删除失败！");
        }

    }
}
